package com.wildwolf.project.wildwolf;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
/**
 * 
 * @author LEON
 *
 */
public class WildWolfApplication extends Application {
	/**
	 * 应用程序的配置项的Key
	 */
    /** Preference key for account username. */
    public static final String ACCOUNT_USERNAME_KEY = "account_username";
    /** Preference key for account password. */
    public static final String ACCOUNT_PASSWORD_KEY = "account_password";
    /** Preference key for status (available, busy, away, ...). */
    public static final String STATUS_KEY = "status";
    /** Preference key for status message. */
    public static final String STATUS_TEXT_KEY = "status_text";
    /** Preference key for connection resource . */
    public static final String CONNECTION_RESOURCE_KEY = "connection_resource";
    /** Preference key for connection priority. */
    public static final String CONNECTION_PRIORITY_KEY = "connection_priority";
    /** Preference key for the use of a proxy. */
    public static final String PROXY_USE_KEY = "proxy_use";
    /** Preference key for the type of proxy. */
    public static final String PROXY_TYPE_KEY = "proxy_type";
    /** Preference key for the proxy server. */
    public static final String PROXY_SERVER_KEY = "proxy_server";
    /** Preference key for the proxy port. */
    public static final String PROXY_PORT_KEY = "proxy_port";
    /** Preference key for the proxy username. */
    public static final String PROXY_USERNAME_KEY = "proxy_username";
    /** Preference key for the proxy password. */
    public static final String PROXY_PASSWORD_KEY = "proxy_password";
    /** Preference key for vibrate on notification. */    
    public static final String NOTIFICATION_VIBRATE_KEY = "notification_vibrate";
    /** Preference key for notification sound. */    
    public static final String NOTIFICATION_SOUND_KEY = "notification_sound";
    /** Preference key for smack debugging. */
    public static final String SMACK_DEBUG_KEY = "smack_debug";
    /** Preference key for full Jid for login. */
    public static final String FULL_JID_LOGIN_KEY = "full_jid_login";
    /** Preference key for display offline contact. */
    public static final String SHOW_OFFLINE_CONTACTS_KEY = "show_offline_contacts";
    /** Preference key for hide the groups. */
    public static final String HIDE_GROUPS_KEY = "hide_groups";
    /** Preference key for auto away enable. */
    public static final String USE_AUTO_AWAY_KEY = "use_auto_away";
    /** Preference key for auto away message. */
    public static final String AUTO_AWAY_MSG_KEY = "auto_away_msg";
    /** Preference key for compact chat ui. */
    public static final String USE_COMPACT_CHAT_UI_KEY = "use_compact_chat_ui";
    /** Preference key for history path on the SDCard. */
    public static final String CHAT_HISTORY_KEY = "settings_chat_history_path";
    /** Preference key to show the jid in the contact list. */
    public static final String SHOW_JID = "show_jid";
    /**
     * 当前会话是否已连接
     */
    private boolean isConnected;
    /**
     * 是否配置了登录信息
     */
    private boolean isAccountConfigured;
    /**
     * 是否允许Pep
     */
    private boolean pepEnabled;
    /**
     * 全局应用程序设置
     */
    private SharedPreferences globalSettings;
    /**
     * 应用程序设置监听器
     */
    private final PreferenceListener globalPreferenceListener = new PreferenceListener();

    /**
     * Constructor.
     */
    public WildWolfApplication() {
    }

    @Override
    public void onCreate() {
	super.onCreate();
	globalSettings = PreferenceManager.getDefaultSharedPreferences(this);
	String login = globalSettings.getString(WildWolfApplication.ACCOUNT_USERNAME_KEY, "");
	String password = globalSettings.getString(WildWolfApplication.ACCOUNT_PASSWORD_KEY, "");
	isAccountConfigured = !("".equals(login) || "".equals(password));
	globalSettings.registerOnSharedPreferenceChangeListener(globalPreferenceListener);
    }

    @Override
    public void onTerminate() {
	super.onTerminate();
	globalSettings.unregisterOnSharedPreferenceChangeListener(globalPreferenceListener);
    }

    /**
     * 
     * @return false断开连接或未建立连接.
     */
    public boolean isConnected() {
	return isConnected;
    }

    public void setConnected(boolean isConnected) {
	this.isConnected = isConnected;
    }

 /**
  * 是否有已配置的登录信息
  * @return
  */
    public boolean isAccountConfigured() {
	return isAccountConfigured;
    }

/**
 * 是否允许Pep
 * @param enabled
 */
    public void setPepEnabled(boolean enabled) {
	this.pepEnabled = enabled;
    }

    public boolean isPepEnabled() {
	return pepEnabled;
    }

	/**
	 * 全局的应用程序配置
	 * 
	 * @author LEON
	 * 
	 */
	private class PreferenceListener implements
			SharedPreferences.OnSharedPreferenceChangeListener {

		public PreferenceListener() {
		}

		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences sharedPreferences, String key) {
			if (WildWolfApplication.ACCOUNT_USERNAME_KEY.equals(key)|| WildWolfApplication.ACCOUNT_PASSWORD_KEY.equals(key)) {
				String login = globalSettings.getString(WildWolfApplication.ACCOUNT_USERNAME_KEY, "");
				String password = globalSettings.getString(WildWolfApplication.ACCOUNT_PASSWORD_KEY, "");
				isAccountConfigured = !("".equals(login) || "".equals(password));
			}
		}
    }
}
